package cn.ibizlab.odoo.core.odoo_mail.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imail_notification;
import cn.ibizlab.odoo.core.odoo_mail.domain.Mail_notification;
import cn.ibizlab.odoo.core.odoo_mail.filter.Mail_notificationSearchContext;
import cn.ibizlab.odoo.core.odoo_mail.service.IMail_notificationService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[消息通知] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mail_notificationServiceImpl implements IMail_notificationService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imail_notificationClientService mail_notificationClientService;

    @Override
    public boolean update(Mail_notification mail_notification) {
        boolean bOk = false ;
        Imail_notification clientModel = this.convert2Model(mail_notification,null);
        mail_notificationClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mail_notification) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Mail_notification> mail_notifications, int batchSize) {

                        //mail_notificationClientService.createBatch(mail_notifications,batchSize) ;

    }

    @Override
    public boolean create(Mail_notification mail_notification) {
        boolean bOk = false ;
        Imail_notification clientModel = this.convert2Model(mail_notification,null);
        mail_notificationClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mail_notification);
        bOk = true ;

        return bOk ;
    }

    @Override
    public Mail_notification get(Integer id) {
        Imail_notification clientModel = mail_notificationClientService.createModel();
        clientModel.setId(id);
        mail_notificationClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mail_notification get(Mail_notification mail_notification) {
        Mail_notification retmail_notification = this.get(mail_notification.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmail_notification.getClass(), mail_notification.getClass(), false);
        copier.copy(retmail_notification, mail_notification, null);
        return mail_notification ;
    }

    @Override
    public void updateBatch(List<Mail_notification> mail_notifications, int batchSize) {

        //mail_notificationClientService.updateBatch(mail_notifications,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imail_notification clientModel = mail_notificationClientService.createModel();
        clientModel.setId(id);
        mail_notificationClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mail_notificationClientService.removeBatch(mail_notifications) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mail_notification> searchDefault(Mail_notificationSearchContext context) {
        List<Mail_notification> list = new ArrayList<Mail_notification>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_notification> clientModelList = mail_notificationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_notification clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_notification>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据寄出邮件查询
     */
    @Override
    public Page<Mail_notification> selectByOdoo_mail(Integer id){
        Mail_notificationSearchContext context = new Mail_notificationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_notification> list = new ArrayList<Mail_notification>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_notification> clientModelList = mail_notificationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_notification clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_notification>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据消息查询
     */
    @Override
    public Page<Mail_notification> selectByOdoo_mail_message(Integer id){
        Mail_notificationSearchContext context = new Mail_notificationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_notification> list = new ArrayList<Mail_notification>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_notification> clientModelList = mail_notificationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_notification clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_notification>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Mail_notification> selectByOdoo_res_partner(Integer id){
        Mail_notificationSearchContext context = new Mail_notificationSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mail_notification> list = new ArrayList<Mail_notification>();
        Page<cn.ibizlab.odoo.core.client.model.Imail_notification> clientModelList = mail_notificationClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imail_notification clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mail_notification>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imail_notification convert2Model(Mail_notification domain , Imail_notification model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mail_notificationClientService.createModel() ;
        }

         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getFailure_reasonDirtyFlag())
            model.setFailure_reason(domain.getFailure_reason());
         if(domain.getIs_emailDirtyFlag())
            model.setIs_email(domain.getIs_email());
         if(domain.getIs_readDirtyFlag())
            model.setIs_read(domain.getIs_read());
         if(domain.getEmail_statusDirtyFlag())
            model.setEmail_status(domain.getEmail_status());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getFailure_typeDirtyFlag())
            model.setFailure_type(domain.getFailure_type());
         if(domain.getRes_partner_id_textDirtyFlag())
            model.setRes_partner_id_text(domain.getRes_partner_id_text());
         if(domain.getMail_idDirtyFlag())
            model.setMail_id(domain.getMail_id());
         if(domain.getMail_message_idDirtyFlag())
            model.setMail_message_id(domain.getMail_message_id());
         if(domain.getRes_partner_idDirtyFlag())
            model.setRes_partner_id(domain.getRes_partner_id());
        return model ;
    }

    public Mail_notification convert2Domain( Imail_notification model ,Mail_notification domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mail_notification();
        }

        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getFailure_reasonDirtyFlag())
            domain.setFailure_reason(model.getFailure_reason());
        if(model.getIs_emailDirtyFlag())
            domain.setIs_email(model.getIs_email());
        if(model.getIs_readDirtyFlag())
            domain.setIs_read(model.getIs_read());
        if(model.getEmail_statusDirtyFlag())
            domain.setEmail_status(model.getEmail_status());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getFailure_typeDirtyFlag())
            domain.setFailure_type(model.getFailure_type());
        if(model.getRes_partner_id_textDirtyFlag())
            domain.setRes_partner_id_text(model.getRes_partner_id_text());
        if(model.getMail_idDirtyFlag())
            domain.setMail_id(model.getMail_id());
        if(model.getMail_message_idDirtyFlag())
            domain.setMail_message_id(model.getMail_message_id());
        if(model.getRes_partner_idDirtyFlag())
            domain.setRes_partner_id(model.getRes_partner_id());
        return domain ;
    }


}
